package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.entity.Prod;
import com.powernode.entity.ProdTag;
import com.powernode.model.Result;
import com.powernode.service.ProdService;
import com.powernode.service.ProdTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品管理控制层
 */
@Api(tags = "商品接口管理")
@RestController
@RequestMapping("prod/prod")
public class ProdController {

    @Resource
    private ProdService prodService;


    @ApiOperation("多条件分页查询商品")
    @GetMapping("page")
    @PreAuthorize("hasAnyAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) String prodName,
                                               @RequestParam(required = false) Integer status){

        Page<Prod> page = new Page<>(current, size);

        page = prodService.page(page,new LambdaQueryWrapper<Prod>()
                .eq(ObjectUtil.isNotEmpty(status),Prod::getStatus,status)
                .like(StrUtil.isNotBlank(prodName),Prod::getProdName,prodName)
                .orderByDesc(Prod::getCreateTime));

        return Result.success(page);
    }


    @ApiOperation("新增商品")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('prod:prod:save')")
    public Result<String> saveProd(@RequestBody Prod prod){
        Boolean saved = prodService.saveProd(prod);
        return Result.handle(saved);

    }


}
